package edu.scau.web.handlerExection;

import com.alipay.api.AlipayApiException;
import edu.scau.core.constants.AjaxResult;
import edu.scau.core.constants.HttpStatus;
import edu.scau.core.exection.BusinessProcessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
@Slf4j
public class ExceptionHandler {
        @org.springframework.web.bind.annotation.ExceptionHandler(MethodArgumentNotValidException.class)
        @ResponseBody
        public AjaxResult handleValidationExceptions(MethodArgumentNotValidException ex) {
            Map<String, String> errors = new HashMap<>();
            String msg="";
            for (ObjectError error : ex.getBindingResult().getAllErrors()) {
                String fieldName = ((FieldError) error).getField();
                String errorMessage= error.getDefaultMessage();
                msg+=errorMessage+",";
                errors.put(fieldName, errorMessage);
            }
            log.warn("Validation failed: {}", errors);
            return AjaxResult.error(HttpStatus.BAD_METHOD, msg);
        }

    @org.springframework.web.bind.annotation.ExceptionHandler(BusinessProcessException.class)
    @ResponseBody
    public AjaxResult handleBusinessProcessException(BusinessProcessException ex) {
        log.warn("BusinessProcessException: {}", ex.getMessage());
        return AjaxResult.error(HttpStatus.ERROR, ex.getMessage());
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public AjaxResult handleIllegalArgumentException(IllegalArgumentException ex) {
        log.warn("IllegalArgumentException: {}", ex.getMessage());
        return AjaxResult.error(HttpStatus.ERROR, ex.getMessage());
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    @ResponseBody
    public AjaxResult handleSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException ex) {
            log.warn("SQLIntegrityConstraintViolationException: {}", ex.getMessage());
            return AjaxResult.error(HttpStatus.ERROR, "操作失败");
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public AjaxResult handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
            log.warn("HttpMessageNotReadableException: {}", ex.getMessage());
            return AjaxResult.error(HttpStatus.ERROR, "请求数据出错！");
    }

}
